package com.example.netchat;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class RoomsContentProvider extends ContentProvider {
	private static final String ROOMS_TYPE = "vnd.android.cursor.dir/vnd.netchat.rooms";
	public static final String ROOMS_PATH = "rooms";
	public static final String AUTHORITY = "com.example.netchat.rooms";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + ROOMS_PATH);
	public static final String[] ALL_COLUMNS = new String[] { "_id",
			"room_name", "room_id", "favorite", "last_reg_id" };
	public static final int ID_COLUMN_INDEX = 0;
	public static final int ROOM_NAME_COLUMN_INDEX = 1;
	public static final int ROOM_ID_COLUMN_INDEX = 2;
	public static final int FAVORITE_COLUMN_INDEX = 3;
	public static final int LAST_REG_ID_COLUMN_INDEX = 4;

	private static final String TABLE_NAME = "rooms";
	private static final int ROOMS_URI = 0;
	private static UriMatcher uriMathcer = new UriMatcher(UriMatcher.NO_MATCH);
	private NetchatDatabase dbHelper;

	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		synchronized (RoomsContentProvider.class) {
			SQLiteDatabase db = dbHelper.getWritableDatabase();

			switch (uriMathcer.match(uri)) {
			case ROOMS_URI:
				int count = db.delete(TABLE_NAME, where, whereArgs);

				if (count > 0) {
					getContext().getContentResolver().notifyChange(CONTENT_URI,
							null);
				}

				return count;
			}

			return 0;
		}
	}

	@Override
	public String getType(Uri uri) {
		switch (uriMathcer.match(uri)) {
		case ROOMS_URI:
			return ROOMS_TYPE;
		}

		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		synchronized (RoomsContentProvider.class) {
			SQLiteDatabase db = dbHelper.getWritableDatabase();

			try {
				switch (uriMathcer.match(uri)) {
				case ROOMS_URI:
					db.insert(TABLE_NAME, "", values);

					getContext().getContentResolver().notifyChange(CONTENT_URI,
							null);

					return CONTENT_URI;
				}
			} finally {
				db.close();
			}

			return null;
		}
	}

	@Override
	public boolean onCreate() {
		dbHelper = new NetchatDatabase(getContext());

		uriMathcer.addURI(AUTHORITY, ROOMS_PATH, ROOMS_URI);

		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String where,
			String[] whereArgs, String sortOrder) {
		synchronized (RoomsContentProvider.class) {
			switch (uriMathcer.match(uri)) {
			case ROOMS_URI:
				SQLiteDatabase db = dbHelper.getReadableDatabase();
				return db.query(TABLE_NAME, projection, where, whereArgs, null,
						null, sortOrder);
			}

			return null;
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String where,
			String[] whereArgs) {
		synchronized (RoomsContentProvider.class) {
			switch (uriMathcer.match(uri)) {
			case ROOMS_URI:
				SQLiteDatabase db = dbHelper.getReadableDatabase();
				try {
					int count = db.update(TABLE_NAME, values, where, whereArgs);

					if (count > 0) {
						getContext().getContentResolver().notifyChange(
								CONTENT_URI, null);
					}

					return count;
				} finally {
					db.close();
				}
			}

			return 0;
		}
	}

}
